package sectorCrack.manager;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.text.ParseException;
import java.util.Date;

import sectorCrack.bean.Dat;
import sectorCrack.utils.GeneralUtils;

public class DataToByteConverter {
	File file;
	int i=0;

	public DataToByteConverter(String filename) {
		file = new File(filename);
	}

	private Dat readDat(String line) throws ParseException {
		line = GeneralUtils.removeLetters(line.trim());
		String[] items = line.split(" ");
		if (items.length < 14) {
			System.out.println(line);
			System.out.println("Not enough values");
			return null;
		}
		for (String item : items) {
			item = item.trim();
		}

		// 0.6 0.62 260.94 261.01 1293.82 1398.91 24.5413677283764 4.6 12.46 2 2
		// 0
		Dat dat = new Dat();
		Date date = null;
		try {
			date = GeneralUtils.getDateFormatter("d/M/yyyy H:mm:ss").parse(items[0] + " " + items[1]);
			//date = GeneralUtils.getDateFormatter("d/M/yyyy H:mm:ss").parse("14/1/2013 00:08:00");

		} catch (ParseException e) {
			date = GeneralUtils.getDateFormatter("yyyy-M-d HH:mm:ss").parse(items[0] + " " + items[1]);

		}
		long epoch = date.getTime() / 1000;
		int timestamp = (int) epoch ;//+ i;
		dat.setTimestamp(timestamp);
		dat.setElevationMax(Float.parseFloat(items[3]));
		dat.setElevationMin(Float.parseFloat(items[2]));
		dat.setAzimuthMax(Float.parseFloat(items[5]));
		dat.setAzimuthMin(Float.parseFloat(items[4]));
		dat.setSlantRangeMax(Float.parseFloat(items[7]));
		dat.setSlantRangeMin(Float.parseFloat(items[6]));
		if (dat.getSlantRangeMax() <= 465) {
			i++;
			System.out.println(i+ "beam++");
		}
		if (Float.parseFloat(items[8]) < -998) {
			dat.setVelocityRaw(Float.NaN);
			dat.setVelocitySpikeFiltered(Float.NaN);
		} else {
			dat.setVelocityRaw(Float.parseFloat(items[8]));
			dat.setVelocitySpikeFiltered(Float.parseFloat(items[8]));
		}
//		if (Float.parseFloat(items[9]) < -998) {
//			dat.setVelocitySpikeFiltered(Float.NaN);
//		} else {
//			dat.setVelocitySpikeFiltered(Float.parseFloat(items[9]));
//		}
		dat.setSnr(Float.parseFloat(items[10]));
		//dat.setSnr(10);
		dat.setCfar(Integer.parseInt(items[11]));
		dat.setMcfar(Integer.parseInt(items[12]));
		dat.setQbit(Integer.parseInt(items[13]));
		return dat;
	}

	public static byte[] datToByteArray(Dat x) {
		ByteBuffer br = ByteBuffer.allocate(52);
		br.order(ByteOrder.LITTLE_ENDIAN);
		br.putInt(x.getTimestamp());
		br.putFloat(x.getElevationMax());
		br.putFloat(x.getElevationMin());
		br.putFloat(x.getAzimuthMax());
		br.putFloat(x.getAzimuthMin());
		br.putFloat(x.getSlantRangeMax());
		br.putFloat(x.getSlantRangeMin());
		br.putFloat(x.getVelocityRaw());
		br.putFloat(x.getVelocitySpikeFiltered());
		br.putFloat(x.getSnr());
		br.putInt(x.getCfar());
		br.putInt(x.getMcfar());
		br.putInt(x.getQbit());

		return br.array();
	}

	public void run() throws ParseException, IOException {
		File outFile = new File(file.getName() + ".out");
		i=0;
		BufferedReader br = new BufferedReader(new FileReader(file));
		FileOutputStream out = new FileOutputStream(outFile);
		String line;
		String prevLine = "";
		int i = 1;
		while ((line = br.readLine()) != null) {
			Dat dat = readDat(line);
			i++;
			if (line.equals(prevLine)) {
				prevLine = line;
				System.out.println("continued " + i);
				continue;
			}
			if (dat != null)
				out.write(datToByteArray(dat));
			prevLine = line;
		}
		br.close();
		out.close();

	}
}
